﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;
using System.Globalization;

namespace Tireguit.Nmon.Parser.Statisitic
{
    public class TOPElement: NmonStatElement
    {
        protected new ILog _log = LogManager.GetLogger(typeof(TOPElement));
        protected IDataParameter _zzzz = null;
        protected IDataParameter _pid = null;
        protected IDataParameter _cpu = null;
        protected IDataParameter _usr = null;
        protected IDataParameter _sys = null;
        protected IDataParameter _threads = null;
        protected IDataParameter _size = null;
        protected IDataParameter _restext = null;
        protected IDataParameter _resdata = null;
        protected IDataParameter _chario = null;
        protected IDataParameter _ram = null;
        protected IDataParameter _paging = null;
        protected IDataParameter _cde = null;
        protected IDataParameter _WLMclass = null;

        public string Zzzz { get; protected set; }
        public string Pid { get; protected set; }
        public float Cpu { get; protected set; }
        public float Usr { get; protected set; }
        public float Sys { get; protected set; }
        public float Threads { get; protected set; }
        public float Size { get; protected set; }
        public float Restext { get; protected set; }
        public float Resdata { get; protected set; }
        public float Chario { get; protected set; }
        public float Ram { get; protected set; }
        public float Paging { get; protected set; }
        public string Cde { get; protected set; }
        public string WLMclass { get; protected set; }



        public TOPElement(IDbConnection connection)
        {
            Balise = BaliseType.PROC;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq = "INSERT INTO " + NmonTables.TablesDictionnary.Instance["TOP"] + " ([zzzz],[Pid],[cpu],[usr],[sys],[threads],[size],[restext],[resdata],[chario],[ram],[paging],[command],[WLMclass]) VALUES (@zzzz,@Pid,@cpu,@usr,@sys,@threads,@size,@restext,@resdata,@chario,@ram,@paging,@command,@WLMclass)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _zzzz = Command.CreateParameter();
            _zzzz.ParameterName = "@zzzz";
            _zzzz.DbType = DbType.String;
            _pid = Command.CreateParameter();
            _pid.ParameterName = "@Pid";
            _pid.DbType = DbType.String;
            _cpu = Command.CreateParameter();
            _cpu.ParameterName = "@cpu";
            _cpu.DbType = DbType.Decimal;
            _usr = Command.CreateParameter();
            _usr.ParameterName = "@usr";
            _usr.DbType = DbType.Decimal;
            _sys = Command.CreateParameter();
            _sys.ParameterName = "@sys";
            _sys.DbType = DbType.Decimal;
            _threads = Command.CreateParameter();
            _threads.ParameterName = "@threads";
            _threads.DbType = DbType.Decimal;
            _size = Command.CreateParameter();
            _size.ParameterName = "@size";
            _size.DbType = DbType.Decimal;
            _restext = Command.CreateParameter();
            _restext.ParameterName = "@restext";
            _restext.DbType = DbType.Decimal;
            _resdata = Command.CreateParameter();
            _resdata.ParameterName = "@resdata";
            _resdata.DbType = DbType.Decimal;
            _chario = Command.CreateParameter();
            _chario.ParameterName = "@chario";
            _chario.DbType = DbType.Decimal;
            _ram = Command.CreateParameter();
            _ram.ParameterName = "@ram";
            _ram.DbType = DbType.Decimal;
            _paging = Command.CreateParameter();
            _paging.ParameterName = "@paging";
            _paging.DbType = DbType.Decimal;
            _cde = Command.CreateParameter();
            _cde.ParameterName = "@command";
            _cde.DbType = DbType.String;
            _WLMclass = Command.CreateParameter();
            _WLMclass.ParameterName = "@WLMclass";
            _WLMclass.DbType = DbType.String;
        }
        public TOPElement() : this(null) { }
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _zzzz.Value = Zzzz;
                _pid.Value = Pid;
                _cpu.Value = Cpu;
                _usr.Value = Usr;
                _sys.Value = Sys;
                _threads.Value = Threads;
                _size.Value = Size;
                _restext.Value = Restext;
                _resdata.Value = Resdata;
                _chario.Value = Chario;
                _ram.Value = Ram;
                _paging.Value = Paging;
                _cde.Value = Cde;
                _WLMclass.Value = WLMclass;
                Command.Parameters.Add(_zzzz);
                Command.Parameters.Add(_pid);
                Command.Parameters.Add(_cpu);
                Command.Parameters.Add(_usr);
                Command.Parameters.Add(_sys);
                Command.Parameters.Add(_threads);
                Command.Parameters.Add(_size);
                Command.Parameters.Add(_restext);
                Command.Parameters.Add(_resdata);
                Command.Parameters.Add(_chario);

                Command.Parameters.Add(_ram);
                Command.Parameters.Add(_paging);
                Command.Parameters.Add(_cde);
                Command.Parameters.Add(_WLMclass);


                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (!elements[0].StartsWith("TOP"))
                {
                    _log.Error("Not TOP Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                if (elements[1].StartsWith("%CPU"))
                {
                    //Skip information header 
                    return false;
                }
                if (elements[1].StartsWith("+PID"))
                {
                    //Skip information header 
                    return false;
                }
                Zzzz = elements[1];
                Pid = elements[2];
                float res = -1;
                
                if (float.TryParse(elements[3], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Cpu = res;

                }
                if (float.TryParse(elements[4], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Usr = res;

                }
                if (float.TryParse(elements[5], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Sys = res;

                }
                if (float.TryParse(elements[6], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Threads = res;

                }
                if (float.TryParse(elements[7], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Size = res;

                }
                if (float.TryParse(elements[8], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Restext = res;

                }
                if (float.TryParse(elements[9], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Resdata = res;

                }
                if (float.TryParse(elements[10], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Chario = res;

                }
                if (float.TryParse(elements[11], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Ram = res;

                }
                if (float.TryParse(elements[12], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Paging = res;

                }
                Cde = elements[13];
                WLMclass = elements[14];

                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}

